package com.wk.springbootlearn.controller;

import com.wk.springbootlearn.common.model.Response;
import com.wk.springbootlearn.pojo.form.GetGroupForm;
import com.wk.springbootlearn.pojo.form.GroupForm;
import com.wk.springbootlearn.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(value = "小组管理", tags = "小组管理模块")
@RestController
@RequestMapping("/group")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @ApiOperation("创建一个小组")
    @PostMapping("/create")
    public Response createGroup(@Valid @RequestBody GroupForm groupForm, @RequestAttribute("userId") String userId) {
        groupService.createGroup(groupForm, new Long(userId));
        return new Response().setOK();
    }

    @ApiOperation("更新一个小组")
    @PutMapping("/update/{id}")
    public Response updateGroup(@PathVariable("id") Long id, @Valid @RequestBody(required = false) GroupForm groupForm, @RequestAttribute("userId") String userId) {
        if (groupForm == null) {
            groupService.updateGroup(id, new GroupForm(), new Long(userId));
        }else {
            groupService.updateGroup(id, groupForm, new Long(userId));
        }
        return new Response().setOK();
    }

    @ApiOperation("分页查询小组列表")
    @PostMapping("/finds")
    public Response findGroup(@Valid @RequestBody(required = false) GetGroupForm getGroupForm) {
        if (getGroupForm == null) {
            return new Response().setData(groupService.findGroup(new GetGroupForm()));
        }
        return new Response().setData(groupService.findGroup(getGroupForm));
    }

    @ApiOperation("删除一个小组")
    @DeleteMapping("/del/{id}")
    public Response deleteGroup(@PathVariable("id") Long id) {
        groupService.delGroup(id);
        return new Response().setOK();
    }
}
